<?php
require_once 'jujucock.php';

define('PORT', 54344);

$sock = socket_create_listen(PORT);
if (false === $sock) {
    echo 'Unable to set option on socket: '. socket_strerror(socket_last_error()) . PHP_EOL;
    exit (1);
}
$ok = socket_set_option($sock, SOL_SOCKET, SO_REUSEADDR, 1);
if (!$ok) {
    echo 'Unable to set option on socket: '. socket_strerror(socket_last_error()) . PHP_EOL;
    exit (1);
}

socket_getsockname($sock, $addr, $port);
print "Server Listening on $addr:$port\n";
$c = socket_accept($sock);
socket_getpeername($c, $raddr, $rport);
print "Received Connection from $raddr:$rport\n";
var_dump($c);
$channel = new JuTCPChannel($c);
   
while( true ) {
   /* do something useful */
   $buf = '';
   $buf_length = 32;
   $length = $channel->recv($buf);
   echo 'RECV: >>>',$buf,"<<<\n";
}
socket_close($c);
socket_close($sock); 
